package com.cetc.nsfx.service.sys;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cetc.nsfx.mapper.RoleMapper;
import com.cetc.nsfx.security.vo.UserContext;
import com.cetc.nsfx.entity.SysAssignPrivs;


@Service
@Transactional
public class RoleService {
	
	 @Autowired
	 private RoleMapper roleMapper;
	 
	 public List<Map<String,Object>> loadUserRightGrid(String nodeid,UserContext user){
			try{
				String rootid ="";
				if("".equals(nodeid) || null == nodeid){
					String userType = null == user.getUser().get("USER_TYPE")?"":user.getUser().get("USER_TYPE").toString();
					if("1".equals(userType)){
						rootid ="23200000000";
					}else{
						rootid = user.getUser().get("SWJG_DM").toString();
					}
				}
				return roleMapper.getSwjgryList(rootid,nodeid);
			}catch(Exception e){
				e.printStackTrace();
			}
			return null;
	    }
	 
	 public List<Map<String,Object>> getUserRolelist(String userLoginName,UserContext user){
	    	
	 	   Map<String,Object> paramMap = new HashMap<String,Object>();
	 	   paramMap.put("currentUser", user.getUserId());
	 	   paramMap.put("userType", user.getUser().get("USER_TYPE"));
	 	   paramMap.put("loginName", userLoginName);
	 	   try{
	 		   return roleMapper.getStationList(paramMap);
	 	   }catch(Exception e){
	 		   e.printStackTrace();
	 	   }
		return null;
	    }
	 
	 @Transactional(readOnly = false)
    public int saveUserRole(String stationID,String userLoginName,String currentUser){
    	
    	int result=0;// 0:保存失败 1：保存成功
    	String [] role = stationID.split("#");
    	String []userId = userLoginName.split(",");
    	List<String> roleidList = new ArrayList<String>();
    	try{
    		List<SysAssignPrivs> list = new ArrayList<SysAssignPrivs>();
    		for(int i =0;i < userId.length;i++){
    			String principalName = userId[i];
    			roleMapper.delByUserRole(principalName,currentUser);
    			String usertype = "0";
    			if(!"".equals(stationID)){
	    			for(int j=0;j<role.length;j++){
	    				SysAssignPrivs assignprivs = new SysAssignPrivs();
	    				assignprivs.setPrincipalName(principalName);
	    				
	    				String []rolestr = role[j].split(",");
	    				
	    				assignprivs.setRoleCode(rolestr[0]);
	    				assignprivs.setIsDelegate(rolestr[1].equals("true")?"1":"0");
	    				assignprivs.setAssignUser(currentUser);
	    				if(rolestr[1].equals("true")){
	    					usertype = "2";
	    				}
	    				list.add(assignprivs);
	    				roleidList.add(rolestr[0]);
	    			}
    			}
    			List<Map<String,Object>> swry = roleMapper.findByLoginNameAndInUse(principalName);
    			Map<String,Object> swryMap = swry.get(0);
    			if(!"1".equals(swryMap.get("USER_TYPE"))){
    				roleMapper.updateUserType(usertype,principalName);
    			}
    		}
    		if(list.size() > 0){
    			roleMapper.saveAssignPrivs(list);
    		}
    		result = 1;
    	}catch(Exception e){
			e.printStackTrace();
    	}
    	return result;
    }
	 
	 public List<Map<String,Object>> getRoleMenulist(String principalName,String userName){
		 return roleMapper.getRoleMenulist(principalName,userName);
	 }
	 
	 public List<Map<String,Object>> getUserRoleList(String principalName){
		 return roleMapper.getUserRoleList(principalName);
	 }
	 
	 public List<Map<String,Object>> getChildMenuList(Map<String,String> param){
		 return roleMapper.getChildMenuList(param);
	 }
	 public List<Map<String,Object>> getUserStationList(String userName,UserContext user){
		 Map<String,String> paramMap = new HashMap<String,String>();
	 	   paramMap.put("currentUser", user.getUserId());
	 	   paramMap.put("loginName", userName);
		 return roleMapper.getUserStationList(paramMap);
	 }
	 
	 public int getUserMenuByMenuCode(String menuCode,String loginName){
		 return roleMapper.getUserMenuByMenuCode(menuCode,loginName);
	 }
}
